package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ProgressStatusForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.EVideo;
import com.jilefojing.beans.query.EVideoQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IEVideoDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IEVideoMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月19日 13时11分12秒
 */
@Repository
public class EVideoDao extends BaseDao<EVideo> implements IEVideoDao {
	@Autowired
	private IEVideoMapper videoMapper;

	@Override
	public Pagination<EVideo> getPagination(EVideoQuery eVideoQuery, Pagination<EVideo> pagination) {
		Example example = new Example(EVideo.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(eVideoQuery.getIds())) {
			criteria.andIn(EVideo.FIELD_ID, eVideoQuery.getIds());
		}

		if (Detect.notEmpty(eVideoQuery.getParentIds())) {
			criteria.andIn(EVideo.FIELD_PARENTID, eVideoQuery.getParentIds());
		}

		if (eVideoQuery.getEntity() != null) {
			if (eVideoQuery.getEntity()) {
				criteria.andEqualTo(EVideo.FIELD_ENTITY, 1);
			} else {
				criteria.andEqualTo(EVideo.FIELD_ENTITY, 0);
			}
		}

		if (Detect.notEmpty(eVideoQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForPublish _Status : eVideoQuery.getStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(EVideo.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(eVideoQuery.getProgressStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (ProgressStatusForSource _Status : eVideoQuery.getProgressStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(EVideo.FIELD_PROGRESSSTATUS, statusCodes);
		}

		if (Detect.notEmpty(eVideoQuery.getTitle())) {
			criteria.andEqualTo(EVideo.FIELD_TITLE, eVideoQuery.getTitle());
		}

		if (Detect.notEmpty(eVideoQuery.getLike_title())) {
			criteria.andLike(EVideo.FIELD_TITLE, "%" + eVideoQuery.getLike_title() + "%");
		}

		if (Detect.notEmpty(eVideoQuery.getTypes())) {
			criteria.andCondition(this.getTypesCondition(eVideoQuery.getTypes()));
		}

		if (Detect.notEmpty(eVideoQuery.getResolutions())) {
			criteria.andCondition(this.getResolutionsCondition(eVideoQuery.getResolutions()));
		}

		if (eVideoQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(EVideo.FIELD_CREATETIME, eVideoQuery.getEqualCreateTime());
		}

		if (eVideoQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EVideo.FIELD_CREATETIME, eVideoQuery.getEqualAndBeforCreateTime());
		}

		if (eVideoQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(EVideo.FIELD_CREATETIME, eVideoQuery.getEqualAndAfterCreateTime());
		}

		if (eVideoQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(EVideo.FIELD_CREATETIME, eVideoQuery.getBeforCreateTime());
		}

		if (eVideoQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(EVideo.FIELD_CREATETIME, eVideoQuery.getAfterCreateTime());
		}

		if (eVideoQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(EVideo.FIELD_UPDATETIME, eVideoQuery.getEqualUpdateTime());
		}

		if (eVideoQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EVideo.FIELD_UPDATETIME, eVideoQuery.getEqualAndBeforUpdateTime());
		}

		if (eVideoQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(EVideo.FIELD_UPDATETIME, eVideoQuery.getEqualAndAfterUpdateTime());
		}

		if (eVideoQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(EVideo.FIELD_UPDATETIME, eVideoQuery.getBeforUpdateTime());
		}

		if (eVideoQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(EVideo.FIELD_UPDATETIME, eVideoQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(eVideoQuery.getCreateUserIds())) {
			criteria.andIn(EVideo.FIELD_CREATEUSER, eVideoQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}

	private String getResolutionsCondition(List<String> resolutions) {
		return this.getTypesCondition(resolutions);
	}

	private String getTypesCondition(List<String> types) {
		// and (so like '' or so like )
		StringBuilder typesBuilder = new StringBuilder();
		typesBuilder.append("( ");
		for (int i = 0; i < types.size(); i++) {
			String type = types.get(i);
			typesBuilder.append(EVideo.COLUMN_SOURCES).append(" like ").append("'%").append(type).append("%'");
			if (i != types.size() - 1) {
				typesBuilder.append(" or ");
			}
		}
		typesBuilder.append(" ) ");
		return typesBuilder.toString();
	}

	//
	// public static void main(String[] args) {
	// List<String> types = new ArrayList<String>();
	// types.add("mp4");
	// // types.add("avi");
	// EVideoDao eVideoDao = new EVideoDao();
	// System.out.println(eVideoDao.getTypesCondition(types));
	// }

	@Override
	public List<EVideo> getListByIds(List<Long> ids) {
		Example example = new Example(EVideo.class);
		example.createCriteria().andIn(EVideo.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public List<EVideo> getListByParentId(long parentId) {
		return this.getListByParentIds(Arrays.asList(new Long[] { parentId }));
	}

	@Override
	public List<EVideo> getListByParentIds(List<Long> parentIds) {
		Example example = new Example(EVideo.class);
		example.createCriteria().andIn(EVideo.FIELD_PARENTID, parentIds);
		example.setOrderByClause(EVideo.ORDER_SORT_DESC);
		return super.getListByExample(example);
	}

	@Override
	public List<Map<String, Integer>> countCognatesByParentIds(List<Long> parentIds) {
		return videoMapper.countCognatesByParentIds(parentIds);
	}

	@Override
	public List<Map<String, Integer>> countCognatePlaytimeByParentIds(List<Long> parentIds) {
		return videoMapper.countCognatePlaytimeByParentIds(parentIds);
	}
}
